package com.listeningframework.boot.autoconfigure.schedule.triggers;

import javax.annotation.PostConstruct;

import org.quartz.Job;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;

import com.listeningframework.boot.exception.BusinessException;
import com.listeningframework.boot.autoconfigure.schedule.overrides.ListeningMethodInvokingJobDetailFactoryBean;

/**
 * 
 * ListeningCronTriggerFactoryBean
 *
 * @author lixining
 * @version $Id: ListeningCronTriggerFactoryBean.java, v 0.1 2016年6月17日 下午5:31:50 lixining Exp $
 */
public abstract class ListeningCronTriggerFactoryBean extends CronTriggerFactoryBean implements ListeningTriggers {
    /**
     * PostConstruct Listening Simple Trigger
     *
     * @throws BusinessException 构造异常
     */
    @PostConstruct
    public void listeningSimpleTriggerPost() throws BusinessException {
        settingJobDetails();
        settingTriggerDetails();
    }

    /**
     * set JobDetailFactoryBean
     *
     * @param job quartz任务
     * @throws BusinessException 设置任务异常
     */
    protected void setSimpleJobDetail(Job job) throws BusinessException {
        try {
            JobDetailFactoryBean details = new JobDetailFactoryBean();
            details.setName(this.getClass().getSimpleName());
            details.setJobClass(job.getClass());
            details.setDurability(true);
            details.afterPropertiesSet();
            this.setJobDetail(details.getObject());
        } catch (Exception e) {
            throw new BusinessException("Setting Simple JobDetail Fail", e);
        }
    }

    /**
     * Set MethodInvokingJobDetailFactoryBean
     * 
     * @param targetObject 目标对象
     * @param targetMethod 目标方法
     * @throws BusinessException 设置异常
     */
    protected void setMethodInvokingJobDetail(Object targetObject, String targetMethod) throws BusinessException {
        try {
            ListeningMethodInvokingJobDetailFactoryBean details = new ListeningMethodInvokingJobDetailFactoryBean();
            details.setName(this.getClass().getSimpleName());
            details.setTargetObject(targetObject);
            details.setTargetMethod(targetMethod);
            details.setConcurrent(false);
            details.afterPropertiesSet();
            this.setJobDetail(details.getObject());
        } catch (Exception e) {
            throw new BusinessException("Setting Method Invoking JobDetail Fail", e);
        }
    }

}
